CC = gcc
CXX = g++
#OPT = -g
OPT = -g -Wall -O3

# Determine the OS we're running on
OSTYPE := $(shell uname)

#
# LINUX SECTION
#
ifneq (,$(findstring Linux,$(OSTYPE)))
DL = -ldl
DLFLAGS = -fpic -shared -Wl,--export-dynamic
endif

#
# MAC OSX SECTION
#
ifneq (,$(findstring Darwin,$(OSTYPE)))
DL = -ldl
DLFLAGS = -fPIC -bundle -undefined dynamic_lookup
endif

#
# CYGWIN SECTION
#
ifneq (,$(findstring cygwin,$(OSTYPE))$(findstring CYGWIN,$(OSTYPE)))
DL =
DLFLAGS = -shared -Wl,--export-dynamic
endif

all: psvn2c abstraction ida_so switchback_so astar_so al_so hida_so

psvn2c: psvn2c.cpp psvn2c.hpp psvn.cpp psvn.hpp
	$(CXX) $(OPT) psvn2c.cpp psvn.cpp -o $@

abstraction: abstraction.cpp psvn.cpp psvn.hpp
	$(CXX) $(OPT) abstraction.cpp psvn.cpp -o $@

pdb.o: pdb.c pdb.h psvn_game_so.h
	$(CC) $(OPT) -c $<

ida_so: ida_so.c psvn_game_so.h pdb.o
	$(CC) $(OPT) ida_so.c pdb.o $(DL) -o $@

astar_data.o: astar_data.cpp astar_data.hpp hash_map.hpp priority_queue.hpp reservoir.hpp
	$(CXX) $(OPT) -c $<

abst_desc.o: abst_desc.cpp abst_desc.hpp psvn_game_so.h
	$(CXX) $(OPT) -c $<

astar_so: astar_so.cpp psvn_game_so.h pdb.o astar_data.o
	$(CXX) $(OPT) astar_so.cpp astar_data.o pdb.o $(DL) -o $@

al_so: al_so.cpp psvn_game_so.h pdb.o astar_data.o
	$(CXX) $(OPT) al_so.cpp astar_data.o pdb.o $(DL) -o $@

switchback_so: switchback_so.cpp switchback.hpp astar_data.o abst_desc.o hash_map.hpp priority_queue.hpp psvn_game_so.h
	$(CXX) $(OPT) switchback_so.cpp astar_data.o abst_desc.o $(DL) -o $@

hida_so: hida_so.cpp abst_desc.o hida.hpp hash_map.hpp psvn_game_so.h
	$(CXX) $(OPT) hida_so.cpp abst_desc.o $(DL) -o $@

%.c: %.psvn psvn2c psvn2c_common.c
	./psvn2c --name=$(*F) --backwards_moves < $< > $@

%.so: %.c psvn_game_so.h
	$(CC) $(OPT) $(DLFLAGS) $< $(DL) -o $@

%.bfs: %.c bfs.cpp priority_queue.hpp
	$(CXX) $(OPT) bfs.cpp -include $< -o $@

%.dynbfs: %.c dynbfs.cpp priority_queue.hpp
	$(CXX) $(OPT) dynbfs.cpp -include $< -o $@

%.brfslk: %.c brfslk.cpp
	$(CXX) $(OPT) brfslk.cpp -include $< -o $@

%.ida: %.c ida.c
	$(CC) $(OPT) ida.c -include $< -o $@

%.dynida: %.c dynida.c
	$(CC) $(OPT) dynida.c -include $< -o $@

%.dynhida: %.c hida.hpp dynhida.cpp dyn_ah.cpp
	$(CXX) $(OPT) dynhida.cpp -include $< -include dyn_ah.cpp -o $@

%.dynsb: %.c switchback.hpp dynsb.cpp dyn_ah.cpp
	$(CXX) $(OPT) dynsb.cpp astar_data.o -include $< -include dyn_ah.cpp -o $@

%.gen_random: %.c gen_random.c
	$(CC) $(OPT) gen_random.c -include $< -o $@

clean:
	rm -f psvn2c abstraction ida_so switchback_so astar_so al_so hida_so *.o
